home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GP_MSDOS.C < prev    next >
C/C++ Source or Header  |  1993-05-12  |  8KB  |  239 lines

  1. /* Copyright (C) 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gp_msdos.c */
  20. /* Common platform-specific routines for MS-DOS (any compiler) */
  21. #include "stdio_.h"
  22. #include <fcntl.h>
  23. #include "dos_.h"
  24. #include "memory_.h"
  25. #include "string_.h"
  26. #include "gstypes.h"
  27. #include "gp.h"
  28. #include "gsutil.h"
  29.  
  30. /* ------ Date and time ------ */
  31.  
  32. /* Read the current date (in days since Jan. 1, 1980) */
  33. /* and time (in milliseconds since midnight). */
  34. void
  35. gp_get_clock(long *pdt)
  36. {    union REGS osdate, ostime;
  37.     long idate;
  38.     static int mstart[12] =
  39.        { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  40.     osdate.h.ah = 0x2a;        /* get date */
  41.     intdos(&osdate, &osdate);
  42. #define da_year rshort.cx
  43. #define da_mon h.dh
  44. #define da_day h.dl
  45.     ostime.h.ah = 0x2c;        /* get time */
  46.     intdos(&ostime, &ostime);
  47. #define ti_hour h.ch
  48. #define ti_min h.cl
  49. #define ti_sec h.dh
  50. #define ti_hund h.dl
  51.     idate = (long)osdate.da_year * 365 +
  52.         (osdate.da_year / 4 + 1 +    /* account for leap years */
  53.          mstart[osdate.da_mon - 1] +    /* month is 1-origin */
  54.          osdate.da_day - 1);        /* day of month is 1-origin */
  55.     if ( osdate.da_mon <= 2 && osdate.da_year % 4 == 0 )        /* Jan. or Feb. of leap year */
  56.         idate--;
  57.     pdt[0] = idate;
  58.     pdt[1] =
  59.         (ostime.ti_hour * 60 + ostime.ti_min) * 60000L +
  60.         (ostime.ti_sec * 100 + ostime.ti_hund) * 10L;
  61. }
  62.  
  63. /* ------ Console management ------ */
  64.  
  65. /* Answer whether a given file is the console (input or output). */
  66. /* This is not a standard gp procedure, */
  67. /* but the MS Windows configuration needs it, */
  68. /* and other MS-DOS configurations might need it someday. */
  69. int
  70. gp_file_is_console(FILE *f)
  71. {    union REGS regs;
  72.     if ( f == NULL )
  73.         return 0;
  74.     regs.h.ah = 0x44;    /* ioctl */
  75.     regs.h.al = 0;        /* get device info */
  76.     regs.rshort.bx = fileno(f);
  77.     intdos(®s, ®s);
  78.     return ((regs.h.dl & 0x80) != 0 && (regs.h.dl & 3) != 0);
  79. }
  80.  
  81. /* ------ Printer accessing ------ */
  82.  
  83. /* Put a printer file (which might be stdout) into binary or text mode. */
  84. /* This is not a standard gp procedure, */
  85. /* but all MS-DOS configurations need it. */
  86. void
  87. gp_set_printer_binary(int prnfno, int binary)
  88. {    union REGS regs;
  89.     regs.h.ah = 0x44;    /* ioctl */
  90.     regs.h.al = 0;        /* get device info */
  91.     regs.rshort.bx = prnfno;
  92.     intdos(®s, ®s);
  93.     if ( binary )
  94.         regs.h.dl |= 0x20;    /* binary (no ^Z intervention) */
  95.     else
  96.         regs.h.dl &= ~0x20;    /* text */
  97.     regs.h.dh = 0;
  98.     regs.h.ah = 0x44;    /* ioctl */
  99.     regs.h.al = 1;        /* set device info */
  100.     intdos(®s, ®s);
  101. }
  102.  
  103. /* ------ File names ------ */
  104.  
  105. /* Define the character used for separating file names in a list. */
  106. const char gp_file_name_list_separator = ';';
  107.  
  108. /* Define the default scratch file name prefix. */
  109. const char gp_scratch_file_name_prefix[] = "_temp_";
  110.  
  111. /* Define the string to be concatenated with the file mode */
  112. /* for opening files without end-of-line conversion. */
  113. const char gp_fmode_binary_suffix[] = "b";
  114. /* Define the file modes for binary reading or writing. */
  115. const char gp_fmode_rb[] = "rb";
  116. const char gp_fmode_wb[] = "wb";
  117.  
  118. /* Answer whether a file name contains a directory/device specification, */
  119. /* i.e. is absolute (not directory- or device-relative). */
  120. int
  121. gp_file_name_is_absolute(const char *fname, uint len)
  122. {    /* A file name is absolute if it contains a drive specification */
  123.     /* (second character is a :) or if it start with / or \. */
  124.     return ( len >= 1 && (*fname == '/' || *fname == '\\' ||
  125.         (len >= 2 && fname[1] == ':')) );
  126. }
  127.  
  128. /* Answer the string to be used for combining a directory/device prefix */
  129. /* with a base file name.  The file name is known to not be absolute. */
  130. const char *
  131. gp_file_name_concat_string(const char *prefix, uint plen,
  132.   const char *fname, uint len)
  133. {    if ( plen > 0 )
  134.       switch ( prefix[plen - 1] )
  135.        {    case ':': case '/': case '\\': return "";
  136.        };
  137.     return "\\";
  138. }
  139.  
  140. /* ------ File enumeration ------ */
  141.  
  142. struct file_enum_s {
  143.     ff_struct_t ffblk;
  144.     char *pattern;            /* orig pattern + modified pattern */
  145.     int patlen;            /* orig pattern length */
  146.     int pat_size;            /* allocate space for pattern */
  147.     int head_size;            /* pattern length through last */
  148.                     /* :, / or \ */
  149.     int first_time;
  150.     const gs_memory_procs *mprocs;
  151. };
  152.  
  153. /* Initialize an enumeration.  Note that * and ? in a directory */
  154. /* don't work, and \ is taken literally unless a second \ follows. */
  155. file_enum *
  156. gp_enumerate_files_init(const char *pat, uint patlen,
  157.   const gs_memory_procs *mprocs)
  158. {    file_enum *pfen = (file_enum *)(*mprocs->alloc)(1, sizeof(file_enum), "gp_enumerate_files");
  159.     int pat_size = 2 * patlen + 1;
  160.     char *pattern;
  161.     char *p;
  162.     int hsize = 0;
  163.     int i;
  164.     if ( pfen == 0 )
  165.         return 0;
  166.     pattern = (*mprocs->alloc)(pat_size, 1,
  167.                    "gp_enumerate_files(pattern)");
  168.     if ( pattern == 0 )
  169.         return 0;
  170.     memcpy(pattern, pat, patlen);
  171.     p = pattern + patlen;
  172.     for ( i = 0; i < patlen; i++ )
  173.     {    switch ( pat[i] )
  174.         {
  175.         case '*':
  176.             /* Skip to . or end of string so DOS can do it. */
  177.             *p++ = '*';
  178.             while ( i < patlen && pat[i] != '.' ) i++;
  179.             i--;
  180.             continue;
  181.         case '\\':
  182.             if ( i + 1 < patlen && pat[i + 1] == '\\' )
  183.                 i++;
  184.             /* falls through */
  185.         case ':':
  186.         case '/':
  187.             hsize = p + 1 - (pattern + patlen);
  188.         }
  189.         *p++ = pat[i];
  190.     }
  191.     *p = 0;
  192.     pfen->pattern = pattern;
  193.     pfen->patlen = patlen;
  194.     pfen->pat_size = pat_size;
  195.     pfen->head_size = hsize;
  196.     pfen->mprocs = mprocs;
  197.     pfen->first_time = 1;
  198.     return pfen;
  199. }
  200.  
  201. /* Enumerate the next file. */
  202. private const string_match_params smp_file = { '*', '?', -1, 1 };
  203. uint
  204. gp_enumerate_files_next(file_enum *pfen, char *ptr, uint maxlen)
  205. {    int code;
  206.     char *p, *q;
  207.     uint len;
  208.     const char *fpat = pfen->pattern + pfen->patlen;
  209. top:    if ( pfen->first_time )
  210.        {    code = dos_findfirst(fpat, &pfen->ffblk);
  211.         pfen->first_time = 0;
  212.        }
  213.     else
  214.         code = dos_findnext(&pfen->ffblk);
  215.     if ( code != 0 )
  216.        {    /* All done, clean up. */
  217.         gp_enumerate_files_close(pfen);
  218.         return ~(uint)0;
  219.        }
  220.     if ( maxlen < 13 + pfen->head_size ) return maxlen + 1;    /* cop out! */
  221.     memcpy(ptr, fpat, pfen->head_size);
  222.     for ( p = &pfen->ffblk.ff_name[0], q = ptr + pfen->head_size; *p; p++ )
  223.         if ( *p != ' ' ) *q++ = *p;
  224.     len = q - ptr;
  225.     /* Make sure this file really matches the pattern. */
  226.     if ( !string_match(ptr, len, pfen->pattern, pfen->patlen, &smp_file) )
  227.         goto top;
  228.     return len;
  229. }
  230.  
  231. /* Clean up the file enumeration. */
  232. void
  233. gp_enumerate_files_close(file_enum *pfen)
  234. {    const gs_memory_procs *mprocs = pfen->mprocs;
  235.     (*mprocs->free)(pfen->pattern, pfen->pat_size, 1,
  236.          "gp_enumerate_files_close(pattern)");
  237.     (*mprocs->free)((char *)pfen, 1, sizeof(file_enum), "gp_enumerate_files_close");
  238. }
  239.